class TreeNode:

    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


from typing import *


class Solution:

    def delNodes(self, root: Optional[TreeNode],
                 to_delete: List[int]) -> List[TreeNode]:
        ans = []

        def f(root, isRoot):
            if root is None:
                return
            if root.val in to_delete:
                f(root.left, True)
                f(root.right, True)
                return

            if isRoot:
                ans.append(root)

            root.left = f(root.left, False)
            root.right = f(root.right, False)
            return root

        f(root, True)
        return ans